package com.game.raceroom.loader
{
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.utils.*;
	
	import com.util.HashMap;
	import com.game.raceroom.player.item.BaseItem;
	import com.game.raceroom.player.item.Car;
	import com.game.raceroom.player.item.Part;

	public class ItemLoader
	{
		[Embed("assets/items.xml", mimeType="application/octet-stream")]
        public static const itemsXml:Class;
        [Embed("assets/parts.xml", mimeType="application/octet-stream")]
        public static const partsXml:Class;
        [Embed("assets/cars.xml", mimeType="application/octet-stream")]
        public static const carsXml:Class;
	    public static var items:HashMap=new HashMap;
	    public static var cars:HashMap=new HashMap;
	    public static var parts:HashMap=new HashMap;
	    public static var allItems:HashMap=new HashMap;
		public function ItemLoader(){

		}
        public static function init(){
			var ba:ByteArray = new itemsXml() as ByteArray;
			
			var xml:XML = new XML( ba.readUTFBytes( ba.length ) );
			loadItem(xml,0,items);
			ba =  new partsXml() as ByteArray;
			xml = new XML( ba.readUTFBytes( ba.length ) );
			loadItem(xml,1,parts);
			ba =   new carsXml() as ByteArray;
			xml = new XML( ba.readUTFBytes( ba.length ) );
			loadItem(xml,2,cars);
		}
 	 
		public static function getItem(id:int):BaseItem{
			if(id<=0)return null;
			return (BaseItem)(allItems.getObjByKey(id)).clone();
		}
		public static function getCar(id:int):Car{
			if(id<=0)return null;
			return (Car)(allItems.getObjByKey(id)).clone();
		}
		public static function getPart(id:int):Part{
			if(id<=0)return null;
			return (Part)(allItems.getObjByKey(id)).clone();
		}
		private static function loadItem(xml:XML,type:int,map:HashMap):void{
			map.clear();
			var id:int=0;
			try {
				// Convert the downlaoded text into an XML instance
				trace(xml.item[0].name);
				for each ( var item:XML in xml.elements() ) {
					if(type==0 ){
					    var baseItem:BaseItem=new BaseItem;
						baseItem.itype=type;
						baseItem.iid=item.id;
						baseItem.idesc=item.desc;
						baseItem.iname=item.name;
						baseItem.iqtt=item.quantity;
						baseItem.ipile=item.pile;
						baseItem.iprice=item.price;
						baseItem.sale=item.sale;
                        map.put(id,baseItem);
                        allItems.put(baseItem.iid,baseItem);
                    }else if(type==1){
					    var part:Part=new Part;
						part.itype=type;
						part.iid=item.id;
						part.idesc=item.desc;
						part.iname=item.name;
						part.iqtt=item.quantity;
						part.ipile=item.pile;
						part.iprice=item.price;
						part.sale=item.sale;
						
					    part.endure=item.endure; 
					    part.speed=item.speed; 
					    part.acccft=item.acccft; 
					    part.bracft=item.bracft; 
					    part.sccft=item.sccft; 
					    part.turncft=item.turncft; 
					    part.confradius=item.confradius; 
					    part.maxatk=item.maxatk; 
					    part.minatk=item.minatk; 
					    part.maxdef=item.maxdef; 
					    part.mindef=item.mindef; 
					    part.fatalatkchance=item.fatalatkchance; 
					    part.unfatalatkchance=item.unfatalatkchance; 
					    part.dodgechance=item.dodgechance; 
					    part.undodgechance=item.undodgechance;
					    part.weight=item.weight; 
					    part.pt=item.pt;
                        map.put(id,part);
                        allItems.put(part.iid,part);
                    }else if(type==2){
					    var car:Car=new Car;
						car.itype=type;
						car.iid=item.id;
						car.idesc=item.desc;
						car.iname=item.name;
						car.iqtt=item.quantity;
						car.ipile=item.pile;
						car.iprice=item.price;
						car.sale=item.sale;
						
					    car.endure=item.endure; 
					    car.speed=item.speed; 
					    car.acccft=item.acccft; 
					    car.bracft=item.bracft; 
					    car.sccft=item.sccft; 
					    car.turncft=item.turncft; 
					    car.confradius=item.confradius; 
					    car.maxatk=item.maxatk; 
					    car.minatk=item.minatk; 
					    car.maxdef=item.maxdef; 
					    car.mindef=item.mindef; 
					    car.fatalatkchance=item.fatalatkchance; 
					    car.unfatalatkchance=item.unfatalatkchance; 
					    car.dodgechance=item.dodgechance; 
					    car.undodgechance=item.undodgechance;
					    car.weight=item.weight; 
                        map.put(id,car);
                        allItems.put(car.iid,car);
                    }
                    id++;
					 
				}

			} catch ( e:TypeError ) {
			}
	    }
	}
}